﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI
{
    public partial class Expenses : BasePage
    {
        protected string SearchFilter
        {
            get { return ViewState["SearchFilter"].ToString(); }
            set { ViewState["SearchFilter"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            lstExpense.OnNeedDataSource += lstExpense_OnNeedDataSource;
            
            if (!IsPostBack)
            {
                Reload();
            }

            if (SessionUser.BranchID != Branches.CurrentBranchID || !SessionUser.Expense)
            {
                btnAddExpense.Enabled = false;
                btnEditExpense.Enabled = false;
                btnDeleteExpense.Enabled = false;

                btnAddExpense.OnClientClick = "";
                btnEditExpense.OnClientClick = "";
                btnDeleteExpense.OnClientClick = "";
            }

        }

        protected void btnSearchExpense_OnClick(object sender, EventArgs e)
        {
            SearchFilter = string.Format("{0} = {1} and {2} = {3}",
                    POSSystem.BLL.Expenses.ColumnNames.Deleted, 0,
                    POSSystem.BLL.Expenses.ColumnNames.BranchID, Branches.CurrentBranchID);

            if (txtSearch.Text != "")
                SearchFilter = string.Format("and ({0} LIKE '%{1}%' or {2} LIKE '%{1}%' or {3} LIKE '%{1}%' or {4} LIKE '%{1}%')",
                    POSSystem.BLL.Expenses.ColumnNames.Title, txtSearch.Text,
                    POSSystem.BLL.Expenses.ColumnNames.Description,
                    POSSystem.BLL.Expenses.ColumnNames.InvoiceNo,
                    POSSystem.BLL.Expenses.ColumnNames.Purchaser);
                
            if (txtDateFrom.Text.Trim() != "")
            {
                SearchFilter += " and " + string.Format("{0} >= '{1} 00:00:00'", POSSystem.BLL.Expenses.ColumnNames.SpentDate, txtDateFrom.Text.Trim());
            }

            if (txtDateTo.Text.Trim() != "")
            {
                SearchFilter += " and " + string.Format("{0} <= '{1} 23:59:00'", POSSystem.BLL.Expenses.ColumnNames.SpentDate, txtDateTo.Text.Trim());
            }
            lstExpense.Refresh();
        }

        private void Reload()
        {
            SearchFilter = string.Format("{0} = 0 and {1} = {2}", POSSystem.BLL.Expenses.ColumnNames.Deleted,
                POSSystem.BLL.Expenses.ColumnNames.BranchID, Branches.CurrentBranchID);
            lstExpense.Refresh();
        }

        protected void lstExpense_OnNeedDataSource(object sender, EventArgs e)
        {
            string sort = lstExpense.SortExpression + " " + lstExpense.SortDirection;
            if (sort.Trim() == "")
                sort = string.Format("{0} desc", POSSystem.BLL.Expenses.ColumnNames.SpentDate);
            POSSystem.BLL.View_Expenses exps = new POSSystem.BLL.View_Expenses();
            int count = exps.Search(SearchFilter, "*", sort, lstExpense.CurrentPageIndex * lstExpense.PageSize, lstExpense.PageSize);

            lstExpense.DataSource = exps.DefaultView;
            lstExpense.VirtualItemCount = count;
        }

        public string GetExpenseEditPageLink()
        {
            return "EditPages/EditExpense.aspx?";
        }

        protected void btnAddExpense_OnClick(Object sender, EventArgs e)
        {
            lstExpense.Refresh();
        }

        protected void btnRemoveExpense_OnClick(object sender, EventArgs e)
        {
            lstExpense.DeleteCheckedItems();
            lstExpense.Refresh();
        }

        protected void btnPrintExpense_OnClick(object sender, EventArgs e)
        {
            POSSystem.BLL.View_Expenses exps = new POSSystem.BLL.View_Expenses();
            exps.Search(SearchFilter, "*", "", 0, 10000);

            ExpenseListCtrl printedList = (ExpenseListCtrl)LoadControl(@"~/Controls/POSControls/ExpenseListCtrl.ascx");
            printedList.AllowPaging = false;
            printedList.AllowSelecting = false;
            printedList.AllowSorting = false;
            printedList.SourceObject = exps;
            printedList.Width = new Unit(100, UnitType.Percentage);
            printedList.GridLineStyle = GridLines.Both;
            printedList.GridBorder = BorderStyle.Solid;
            printedList.GridBorderWidth = new Unit(1, UnitType.Pixel);
            printedList.CssClass = "";
            printedList.Refresh();

            Session[PrintKey] = printedList;
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showPrintDlg('{0}');", GetPrintPage()), true);

            lstExpense.Refresh();
        }

        protected void btnExcel_OnClick(object sender, EventArgs e)
        {
            POSSystem.BLL.View_Expenses exps = new POSSystem.BLL.View_Expenses();
            int count  = exps.Search(SearchFilter, "*", "", 0, 10000);
            if(count > 0)
                Utilities.ExportToExcel(exps, "Expenses.xls",
                    new String[] { "Title", "Amount", "Description", "Purchaser", "SpentDate", "PaymentMethod"},
                    new String[] { "Title", "Amount", "Description", "Purchaser", "SpentDate", "PaymentMethod" },
                    "Expenses");
        }

        protected void btnReload_OnClick(object sender, EventArgs e)
        {
            Reload();
        }

        public string PrintKey
        {
            get
            {
                if (ViewState["PrintKey"] == null)
                    ViewState["PrintKey"] = Guid.NewGuid().ToString();
                return ViewState["PrintKey"].ToString();
            }
        }

        public string GetPrintPage()
        {
            return "EditPages/PrintHandler.aspx?key=" + PrintKey;
        }
    }
}
